home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 59 / 59.xpi / chrome / useragentswitcher.jar / content / useragentswitcher / xml / import.js < prev    next >
Text File  |  2009-06-30  |  17KB  |  567 lines

  1. // User Agent Switcher importer
  2. var UserAgentSwitcherImporter =
  3. {
  4.     importType:        0,
  5.     importTypeMenu:    1,
  6.     importTypeOptions: 2,
  7.     
  8.     folderCount:    0,
  9.     separatorCount: 0,
  10.     userAgentCount: 0,
  11.     
  12.     menuParentFolder:    null,
  13.     optionsParentFolder: null,
  14.     toolbarParentFolder: null,
  15.  
  16.     importDocument: null,
  17.  
  18.     // Adds a menu folder
  19.     addMenuFolder: function(menu, suffix, parentFolder)
  20.     {
  21.         var newMenu = menu.cloneNode(true);
  22.  
  23.         newMenu.setAttribute("id", "useragentswitcher-folder-" + this.folderCount + "-" + suffix);
  24.  
  25.         // If the parent folder is set
  26.         if(parentFolder)
  27.         {
  28.             parentFolder.firstChild.appendChild(newMenu);
  29.         }
  30.         else
  31.         {
  32.             var menu             = this.importDocument.getElementById("useragentswitcher-popup-" + suffix);
  33.             var optionsSeparator = this.importDocument.getElementById("useragentswitcher-separator-2-" + suffix);
  34.         
  35.             // If the menu and options separator exist
  36.             if(menu && optionsSeparator)
  37.             {        
  38.                 menu.insertBefore(newMenu, optionsSeparator);
  39.             }        
  40.         }
  41.         
  42.         return newMenu;
  43.     },
  44.  
  45.     // Adds a menu separator
  46.     addMenuSeparator: function(menuSeparator, suffix, parentFolder)
  47.     {
  48.         var newMenuSeparator = menuSeparator.cloneNode(false);
  49.  
  50.         newMenuSeparator.setAttribute("id", "useragentswitcher-generated-separator-" + this.separatorCount + "-" + suffix);
  51.  
  52.         // If the parent folder is set
  53.         if(parentFolder)
  54.         {
  55.             parentFolder.firstChild.appendChild(newMenuSeparator);
  56.         }
  57.         else
  58.         {
  59.             var menu             = this.importDocument.getElementById("useragentswitcher-popup-" + suffix);
  60.             var optionsSeparator = this.importDocument.getElementById("useragentswitcher-separator-2-" + suffix);
  61.         
  62.             // If the menu and options separator exist
  63.             if(menu && optionsSeparator)
  64.             {        
  65.                 menu.insertBefore(newMenuSeparator, optionsSeparator);
  66.             }        
  67.         }
  68.     },
  69.  
  70.     // Adds a menu user agent
  71.     addMenuUserAgent: function(menuItem, suffix, parentFolder)
  72.     {
  73.         var newMenuItem = menuItem.cloneNode(false);
  74.  
  75.         newMenuItem.setAttribute("id", "useragentswitcher-user-agent-" + this.userAgentCount + "-" + suffix);
  76.         newMenuItem.setAttribute("name", "useragentswitcher-group-" + suffix);        
  77.  
  78.         // If the parent folder is set
  79.         if(parentFolder)
  80.         {
  81.             parentFolder.firstChild.appendChild(newMenuItem);
  82.         }
  83.         else
  84.         {
  85.             var menu             = this.importDocument.getElementById("useragentswitcher-popup-" + suffix);
  86.             var optionsSeparator = this.importDocument.getElementById("useragentswitcher-separator-2-" + suffix);
  87.         
  88.             // If the menu and options separator exist
  89.             if(menu && optionsSeparator)
  90.             {        
  91.                 menu.insertBefore(newMenuItem, optionsSeparator);
  92.             }        
  93.         }
  94.     },
  95.  
  96.     // Adds an option folder
  97.     addOptionsFolder: function(treeItem)
  98.     {
  99.         // If the parent folder is set
  100.         if(this.optionsParentFolder)
  101.         {
  102.             this.optionsParentFolder.appendChild(treeItem);
  103.         }
  104.         else
  105.         {
  106.             this.importDocument.getElementById("useragentswitcher-options-user-agents").appendChild(treeItem);
  107.         }
  108.     },
  109.  
  110.     // Adds an options separator
  111.     addOptionsSeparator: function(treeSeparator)
  112.     {
  113.         // If the parent folder is set
  114.         if(this.optionsParentFolder)
  115.         {
  116.             this.optionsParentFolder.appendChild(treeSeparator);
  117.         }
  118.         else
  119.         {
  120.             this.importDocument.getElementById("useragentswitcher-options-user-agents").appendChild(treeSeparator);
  121.         }
  122.     },
  123.  
  124.     // Adds an option user agent
  125.     addOptionsUserAgent: function(treeItem)
  126.     {
  127.         // If the parent folder is set
  128.         if(this.optionsParentFolder)
  129.         {
  130.             this.optionsParentFolder.appendChild(treeItem);
  131.         }
  132.         else
  133.         {
  134.             this.importDocument.getElementById("useragentswitcher-options-user-agents").appendChild(treeItem);
  135.         }
  136.     },
  137.  
  138.     // Creates the user agent directory
  139.     createUserAgentDirectory: function()
  140.     {
  141.         var userAgentDirectory = this.getUserAgentDirectoryLocation();
  142.         
  143.       userAgentDirectory.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0755);
  144.     },
  145.  
  146.     // Creates the user agent file
  147.     createUserAgentFile: function()
  148.     {
  149.         var userAgentFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
  150.  
  151.         userAgentFile.initWithPath(this.getUserAgentFileLocation().path);
  152.         userAgentFile.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 00644);
  153.         
  154.         return userAgentFile;
  155.     },
  156.  
  157.     // Evaluates the specified xpath on the specified element returning an array of the matching elements
  158.     evaluateXPath: function(expression, element)
  159.     {
  160.         var evaluationResults = null;
  161.         var matchingElement   = null;
  162.         var matchingElements  = [];
  163.         var ownerDocument     = element.ownerDocument;
  164.         var xPathEvaluator    = new XPathEvaluator();
  165.         var resolver          = xPathEvaluator.createNSResolver(element.ownerDocument == null ? element.documentElement : element.ownerDocument.documentElement);
  166.     
  167.         evaluationResults = xPathEvaluator.evaluate(expression, element, resolver, 0, null);
  168.     
  169.         // While there are more matching elements
  170.         while((matchingElement = evaluationResults.iterateNext()) != null)
  171.         {
  172.             matchingElements.push(matchingElement);
  173.         }
  174.     
  175.         return matchingElements;
  176.     },
  177.  
  178.     // Returns the counts for existing user agents
  179.     getExistingCounts: function()
  180.     {
  181.         var existingCounts = {};
  182.         var userAgentTree  = this.importDocument.getElementById("useragentswitcher-options-user-agents");
  183.     
  184.         existingCounts.folderCount    = UserAgentSwitcherDOM.findElementsByXPath(userAgentTree, "//treeitem[@container='true']").length;
  185.         existingCounts.separatorCount = UserAgentSwitcherDOM.findElementsByXPath(userAgentTree, "//treeseparator").length;
  186.         existingCounts.userAgentCount = UserAgentSwitcherDOM.findElementsByXPath(userAgentTree, "//treeitem[not(@container)]").length;
  187.     
  188.         return existingCounts;
  189.     },
  190.  
  191.     // Returns the user agent directory location
  192.     getUserAgentDirectoryLocation: function()
  193.     {
  194.         var directory = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsILocalFile);
  195.  
  196.         directory.append("useragentswitcher");
  197.         
  198.         return directory;
  199.     },
  200.  
  201.     // Returns the user agent file location
  202.     getUserAgentFileLocation: function()
  203.     {
  204.         var file = this.getUserAgentDirectoryLocation();
  205.         
  206.         file.append("useragents.xml");
  207.         
  208.         return file;
  209.     },
  210.  
  211.     // Imports the user agents from a file
  212.     import: function(type, file, ignoreParentWindow)
  213.     {
  214.         // Try to import from a file
  215.         try
  216.         {
  217.             // If the file exists, is a file and is readable
  218.             if(file.exists() && file.isFile() && file.isReadable())
  219.             {
  220.                 var request     = new XMLHttpRequest();
  221.                 var xmlDocument = null;
  222.     
  223.                 request.open("get", "file://" + file.path, false);
  224.                 request.send(null);
  225.     
  226.                 xmlDocument = request.responseXML;
  227.                 
  228.                 // If the file could not be parsed correctly
  229.                 if(xmlDocument.documentElement.nodeName == "parsererror")
  230.                 {
  231.                     return UserAgentSwitcherStringBundle.getFormattedString("importParserError", [file.path]);
  232.                 }
  233.                 else
  234.                 {
  235.                     this.folderCount    = 0;
  236.                    this.importType     = type;
  237.                    this.parentFolder   = null;
  238.                     this.separatorCount = 0;
  239.                     this.userAgentCount = 0;
  240.  
  241.                     // If we are importing from the options
  242.                     if(type == this.importTypeOptions)
  243.                     {
  244.                         this.importDocument = document;
  245.                     
  246.                         // If the overwrite preference is set
  247.                         if(this.importDocument.getElementById("useragentswitcher-import-overwrite").checked)
  248.                         {
  249.                             UserAgentSwitcherDOM.removeAllChildElements(this.importDocument.getElementById("useragentswitcher-options-user-agents"));    
  250.                         }
  251.                         else
  252.                         {
  253.                             var existingCounts = this.getExistingCounts();
  254.                         
  255.                             this.folderCount    = existingCounts.folderCount;
  256.                             this.separatorCount = existingCounts.separatorCount;
  257.                             this.userAgentCount = existingCounts.userAgentCount;
  258.                         }
  259.                     }
  260.                     else
  261.                     {
  262.                         this.setImportDocument(ignoreParentWindow);
  263.                         this.removeUserAgents();    
  264.                     }
  265.     
  266.                     this.importFile(xmlDocument.documentElement);
  267.                     
  268.                     // If nothing was imported
  269.                     if(this.separatorCount == 0 && this.folderCount == 0 && this.userAgentCount == 0)
  270.                     {
  271.                         return UserAgentSwitcherStringBundle.getFormattedString("importParserError", [file.path]);
  272.                     }
  273.                 }
  274.             }
  275.             else
  276.             {
  277.                 return UserAgentSwitcherStringBundle.getFormattedString("importFileFailed", [file.path]);
  278.             }
  279.         }
  280.         catch(exception)
  281.         {
  282.             UserAgentSwitcherLog.log("UserAgentSwitcherImporter.import", exception);
  283.         }
  284.  
  285.         return null;
  286.     },
  287.     
  288.     // Imports the user agents
  289.     importFile: function(rootNode)
  290.     {
  291.         var element        = null;
  292.         var elements       = this.evaluateXPath("*", rootNode);
  293.         var elementsLength = elements.length;
  294.  
  295.         // Loop through the user agents
  296.         for(var i = 0; i < elementsLength; i++)
  297.         {
  298.             element = elements[i];
  299.  
  300.             // If this is a user agent
  301.             if(element.nodeName == "useragent")
  302.             {
  303.                 this.userAgentCount++;
  304.  
  305.                 this.importUserAgent(element);                
  306.             }
  307.             else if(element.nodeName == "folder")
  308.             {
  309.                 this.folderCount++;                
  310.  
  311.                 this.importFolder(element);                
  312.             }
  313.             else if(element.nodeName == "separator")
  314.             {
  315.                 this.separatorCount++;
  316.  
  317.                 this.importSeparator();                
  318.             }
  319.         }
  320.     },
  321.     
  322.     // Imports a folder
  323.     importFolder: function(folderElement)
  324.     {
  325.         // If this is a menu import type
  326.         if(this.importType == this.importTypeMenu)
  327.         {
  328.             var menu                        = this.importDocument.createElement("menu");
  329.             var previousMenuParentFolder    = this.menuParentFolder;
  330.             var previousToolbarParentFolder = this.toolbarParentFolder;
  331.     
  332.             // If the folder element has a description attribute
  333.             if(folderElement.hasAttribute("description"))
  334.             {
  335.                 menu.setAttribute("label", folderElement.getAttribute("description"));
  336.             }
  337.     
  338.             menu.appendChild(this.importDocument.createElement("menupopup"));
  339.             
  340.             this.menuParentFolder    = this.addMenuFolder(menu, "menu", this.menuParentFolder);    
  341.             this.toolbarParentFolder = this.addMenuFolder(menu, "toolbar", this.toolbarParentFolder);    
  342.                 
  343.             this.importFile(folderElement);
  344.             
  345.             this.menuParentFolder    = previousMenuParentFolder;    
  346.             this.toolbarParentFolder = previousToolbarParentFolder;    
  347.         }
  348.         else if(this.importType == this.importTypeOptions)
  349.         {
  350.             var previousParentFolder = this.optionsParentFolder;
  351.             var treeCell             = this.importDocument.createElement("treecell");
  352.             var treeChildren         = this.importDocument.createElement("treechildren");
  353.             var treeItem             = this.importDocument.createElement("treeitem");
  354.             var treeRow              = this.importDocument.createElement("treerow");
  355.     
  356.             // If the folder element has a description attribute
  357.             if(folderElement.hasAttribute("description"))
  358.             {
  359.                 treeCell.setAttribute("label", folderElement.getAttribute("description"));
  360.             }
  361.  
  362.             treeChildren.setAttribute("id", "useragentswitcher-options-folder-" + this.folderCount);
  363.             treeItem.setAttribute("container", true);
  364.             treeRow.appendChild(treeCell);
  365.             treeItem.appendChild(treeRow);
  366.             treeItem.appendChild(treeChildren);
  367.             
  368.             this.addOptionsFolder(treeItem);    
  369.             
  370.             this.optionsParentFolder = treeChildren;
  371.                 
  372.             this.importFile(folderElement);
  373.             
  374.             this.optionsParentFolder = previousParentFolder
  375.         }
  376.     },
  377.     
  378.     // Imports a separator
  379.     importSeparator: function()
  380.     {
  381.         // If this is a menu import type
  382.         if(this.importType == this.importTypeMenu)
  383.         {
  384.             var menuSeparator = this.importDocument.createElement("menuseparator");
  385.     
  386.             this.addMenuSeparator(menuSeparator, "menu", this.menuParentFolder);    
  387.             this.addMenuSeparator(menuSeparator, "toolbar", this.toolbarParentFolder);    
  388.         }
  389.         else if(this.importType == this.importTypeOptions)
  390.         {
  391.             this.addOptionsSeparator(this.importDocument.createElement("treeseparator"));    
  392.         }
  393.     },
  394.     
  395.     // Imports a user agent
  396.     importUserAgent: function(userAgentElement)
  397.     {
  398.         // If the user agent element has a description attribute
  399.         if(userAgentElement.hasAttribute("description"))
  400.         {
  401.             // If this is a menu import type
  402.             if(this.importType == this.importTypeMenu)
  403.             {
  404.                 var menuItem = this.importDocument.createElement("menuitem");
  405.     
  406.                 this.populateUserAgent(menuItem, userAgentElement);
  407.     
  408.                 menuItem.setAttribute("oncommand", "UserAgentSwitcher.switchUserAgent(this)");
  409.                 menuItem.setAttribute("type", "radio");
  410.                 menuItem.setAttribute("useragentswitcherposition", this.userAgentCount);
  411.         
  412.                 this.addMenuUserAgent(menuItem, "menu", this.menuParentFolder);    
  413.                 this.addMenuUserAgent(menuItem, "toolbar", this.toolbarParentFolder);    
  414.             }
  415.             else if(this.importType == this.importTypeOptions)
  416.             {
  417.                 var treeCell = this.importDocument.createElement("treecell");
  418.                 var treeItem = this.importDocument.createElement("treeitem");
  419.                 var treeRow  = this.importDocument.createElement("treerow");
  420.     
  421.                 this.populateUserAgent(treeCell, userAgentElement);
  422.     
  423.                 treeRow.appendChild(treeCell);
  424.                 treeItem.appendChild(treeRow);
  425.  
  426.                 this.addOptionsUserAgent(treeItem);    
  427.             }
  428.         }
  429.     },
  430.     
  431.     // Populates a user agent
  432.     populateUserAgent: function(userAgent, userAgentElement)
  433.     {
  434.         userAgent.setAttribute("label", userAgentElement.getAttribute("description"));
  435.  
  436.         // If the user agent element has an app code name attribute
  437.         if(userAgentElement.hasAttribute("appcodename"))
  438.         {
  439.             userAgent.setAttribute("useragentswitcherappcodename", userAgentElement.getAttribute("appcodename"));
  440.         }
  441.         else
  442.         {
  443.             userAgent.setAttribute("useragentswitcherappcodename", "");
  444.         }
  445.  
  446.         // If the user agent element has an app name attribute
  447.         if(userAgentElement.hasAttribute("appname"))
  448.         {
  449.             userAgent.setAttribute("useragentswitcherappname", userAgentElement.getAttribute("appname"));
  450.         }
  451.         else
  452.         {
  453.             userAgent.setAttribute("useragentswitcherappname", "");
  454.         }
  455.  
  456.         // If the user agent element has an app version attribute
  457.         if(userAgentElement.hasAttribute("appversion"))
  458.         {
  459.             userAgent.setAttribute("useragentswitcherappversion", userAgentElement.getAttribute("appversion"));
  460.         }
  461.         else
  462.         {
  463.             userAgent.setAttribute("useragentswitcherappversion", "");
  464.         }
  465.  
  466.         // If the user agent element has a platform attribute
  467.         if(userAgentElement.hasAttribute("platform"))
  468.         {
  469.             userAgent.setAttribute("useragentswitcherplatform", userAgentElement.getAttribute("platform"));
  470.         }
  471.         else
  472.         {
  473.             userAgent.setAttribute("useragentswitcherplatform", "");
  474.         }
  475.  
  476.         // If the user agent element has a useragent attribute
  477.         if(userAgentElement.hasAttribute("useragent"))
  478.         {
  479.             userAgent.setAttribute("useragentswitcheruseragent", userAgentElement.getAttribute("useragent"));
  480.         }
  481.         else
  482.         {
  483.             userAgent.setAttribute("useragentswitcheruseragent", "");
  484.         }
  485.  
  486.         // If the user agent element has a vendor attribute
  487.         if(userAgentElement.hasAttribute("vendor"))
  488.         {
  489.             userAgent.setAttribute("useragentswitchervendor", userAgentElement.getAttribute("vendor"));
  490.         }
  491.         else
  492.         {
  493.             userAgent.setAttribute("useragentswitchervendor", "");
  494.         }
  495.  
  496.         // If the user agent element has a vendor sub attribute
  497.         if(userAgentElement.hasAttribute("vendorsub"))
  498.         {
  499.             userAgent.setAttribute("useragentswitchervendorsub", userAgentElement.getAttribute("vendorsub"));
  500.         }
  501.         else
  502.         {
  503.             userAgent.setAttribute("useragentswitchervendorsub", "");
  504.         }
  505.     },
  506.     
  507.     // Removes the user agents from the menu
  508.     removeMenuUserAgents: function(suffix)
  509.     {
  510.         var optionsSeparator = this.importDocument.getElementById("useragentswitcher-separator-1-" + suffix);
  511.  
  512.         // If the options separator exists
  513.         if(optionsSeparator)
  514.         {        
  515.             // Remove the next sibling to the top separator if it exists and is not the bottom separator
  516.             while(optionsSeparator.nextSibling && optionsSeparator.nextSibling.getAttribute("id") != "useragentswitcher-separator-2-" + suffix)
  517.             {
  518.                 UserAgentSwitcherDOM.removeElement(optionsSeparator.nextSibling);
  519.             }
  520.         }
  521.     },
  522.     
  523.     // Removes the user agents
  524.     removeUserAgents: function()
  525.     {
  526.         this.removeMenuUserAgents("menu");
  527.         this.removeMenuUserAgents("toolbar");
  528.     },
  529.     
  530.     // Resets the user agent file
  531.     reset: function()
  532.     {
  533.         var outputStream  = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
  534.         var request       = new XMLHttpRequest();
  535.         var userAgentFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
  536.  
  537.         userAgentFile.initWithPath(this.getUserAgentFileLocation().path);
  538.  
  539.         request.open("get", "chrome://useragentswitcher/content/xml/useragents.xml", false);
  540.         request.send(null);
  541.  
  542.         outputStream.init(userAgentFile, 0x04 | 0x08 | 0x20, 00644, null);
  543.         outputStream.write(request.responseText, request.responseText.length);
  544.         outputStream.close();    
  545.     },
  546.  
  547.     // Sets the import document
  548.     setImportDocument: function(ignoreParentWindow)
  549.     {
  550.         this.importDocument = document;
  551.     
  552.         // If not ignoring the parent window and there is a parent window
  553.         if(!ignoreParentWindow && window.opener)
  554.         {
  555.             // If there is a grand parent window and it has the extension menu
  556.             if(window.opener.opener && window.opener.opener.document.getElementById("useragentswitcher-menu"))
  557.             {
  558.                 this.importDocument = window.opener.opener.document;
  559.             }
  560.             else
  561.             {
  562.                 this.importDocument = window.opener.document;
  563.             }
  564.         }
  565.     }
  566. };
  567.